Does the number of bits used for a primitive variable ever change?
No—a primitive variable is always the same data type, which means the same number of bits.
(When you read about objects, you will find that
objects of a particular type do consist of varying numbers of bytes.
Ignore this for now.)
Here is a program that uses the variable payAmount
.
class example
{
public static void main ( String[] args )
{
long payAmount = 123; //a declaration of a variable
System.out.println("The variable contains: " + payAmount );
}
}
The line long payAmount = 123;
is a declaration
of a variable.
A declaration of a variable is where the program
says that a variable will be needed.
For our small programs, declaration statements
will be placed between the two braces of the main
method.
The declaration gives a name and a data type for the variable.
It may also ask that a particular value be placed in the variable.
In a high level language (such as Java)
the programmer does not need to worry about how the computer hardware
actually does what was asked.
If you ask for a variable of type long
, you get it.
Details like bytes and memory addresses are up to the Java compiler.
The declation in the example program requested a 64 bit section of memory
which will use primitive data type long
and will be named payAmount
.
Initially, the variable will have the value 123 stored in it.
The compiler and java interpreter will (later on) make sure that this
happens.
A variable cannot be used in a program unless it has been declared. A variable can be declared only once.